<?php
/**
* @file $Id: StudentFields.php 580 2007-06-05 19:19:11Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/
?>
<script language=javascript>
function changeDataType(type)
{
	switch(type)
	{
		case 'radio':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=checkbox name=tables[new][DEFAULT_SELECTION] value=Y>';
		break;

		case 'text':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'autos':
		case 'codeds':
		case 'select':
			document.getElementById('options_div').innerHTML = '<?php echo TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','Pull-Down/Select Multiple Choices<BR>* one per line','rows=7 cols=40'); ?>';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'numeric':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=4>';
		break;

		case 'multiple':
			document.getElementById('options_div').innerHTML = '<?php echo TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','Pull-Down/Select Multiple Choices<BR>* one per line','rows=7 cols=40'); ?>';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'date':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10><small>* YYYY-MM-DD</small>';
		break;

		case 'textarea':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<TEXTAREA name=tables[new][DEFAULT_SELECTION] cols=20 rows=4></TEXTAREA>';
		break;

		case 'log':
			document.getElementById('options_div').innerHTML = '<?php
			$div = false;
			$new = true;
			$header .= '<small>Setup the Logging Fields:</small><TABLE style="border-style:none none none none;" cellspacing=0 callpadding=0 border=1 width=100%><TR>';
			// COPY TO LOWER SECTION STARTING HERE 
			$header .= '<TD valign=top style="border-color:transparent;border-style:none solid solid none;" bgcolor=#333366><IMG SRC=assets/left_corner.gif><small><B><font color=#FFFFFF>'._('Use Field?').'</font></b></small>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Checkbox').'</font></small><BR>'.CheckBoxInput($RET['LOG_CHECKBOX'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Pull-down').'</font></small><BR>'.CheckBoxInput($RET['LOG_SELECT'],'tables['.$_REQUEST['id'].'][LOG_SELECT]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Date').'</font></small><BR>'.CheckBoxInput($RET['LOG_DATE'],'tables['.$_REQUEST['id'].'][LOG_DATE]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Numeric').'</font></small><BR>'.CheckBoxInput($RET['LOG_NUMERIC'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 1').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT1'],'tables['.$_REQUEST['id'].'][LOG_TEXT1]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 2').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT2'],'tables['.$_REQUEST['id'].'][LOG_TEXT2]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 3').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT3'],'tables['.$_REQUEST['id'].'][LOG_TEXT3]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 4').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT4'],'tables['.$_REQUEST['id'].'][LOG_TEXT4]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Long Text').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXTAREA'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA]','','',$new).'</TD>';

			$header .= '</TR><TR>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;"><small><b>'._('Field Title').'</b></small></TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_CHECKBOX_TITLE'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX_TITLE]','','',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_SELECT_TITLE'],'tables['.$_REQUEST['id'].'][LOG_SELECT_TITLE]','','',$div).'<BR>';
			$header .= '<small><b>Pull-Down Choices:</b></small>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','* '._('one per line'),'cols=10 rows=3');
			$header .= '</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_DATE_TITLE'],'tables['.$_REQUEST['id'].'][LOG_DATE_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_NUMERIC_TITLE'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT1_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT1_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT2_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT2_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT3_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT3_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT4_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT4_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXTAREA_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA_TITLE]','','size=8',$div).'</TD>';
			// END COPY
			$header .= '</TR></TABLE>';
			echo $header;
			$header = '';
				 ?>';
				
			document.getElementById('default_div').innerHTML = 'N/A';
		break;
	}
}
</script>
<?php

DrawHeader(ProgramTitle());
$_FOCUS['allow_edit'] = true;

if($_REQUEST['tables'] && $_POST['tables'])
{
	$table = $_REQUEST['table'];
	foreach($_REQUEST['tables'] as $id=>$columns)
	{
		if($id!='new')
		{
			$old_sort_order = DBGet(DBQuery("SELECT SORT_ORDER FROM CUSTOM_FIELDS WHERE ID='".$id."'"));
			$old_sort_order = $old_sort_order[1]['SORT_ORDER'];
			if($table=='CUSTOM_FIELDS' && $columns['SORT_ORDER'])
			{
				_rearrangeSortOrders($_REQUEST['category_id'],$id,$old_sort_order,$columns['SORT_ORDER']);
				unset($columns['SORT_ORDER']);
			}
			if($columns['CATEGORY_ID'] && $columns['CATEGORY_ID']!=$_REQUEST['category_id'])
			{
				// delete from current category
				_rearrangeSortOrders($_REQUEST['category_id'],$id,$old_sort_order,false);
				$_REQUEST['category_id'] = $columns['CATEGORY_ID'];
				// add at the end of the new category
				_rearrangeSortOrders($_REQUEST['category_id'],$id,false,-1);
			}

			if(count($columns))
			{
				$sql = "UPDATE $table SET ";
	
				foreach($columns as $column=>$value)
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				$sql = substr($sql,0,-1) . " WHERE ID='$id'";
				$go = true;
			}
		}
		else
		{
			$sql = "INSERT INTO $table ";
			
			if($table=='CUSTOM_FIELDS')
			{
				if($columns['TYPE']=='holder')
					unset($columns['TITLE']);
				if($columns['CATEGORY_ID'])
				{
					$_REQUEST['category_id'] = $columns['CATEGORY_ID'];
					unset($columns['CATEGORY_ID']);
				}
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('CUSTOM_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,CATEGORY_ID,";
				$values = $id.",'".$_REQUEST['category_id']."',";
				$_REQUEST['id'] = $id;

				switch($columns['TYPE'])
				{
					case 'radio':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id VARCHAR(1)");
					break;
					
					case 'text':
					case 'select':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id VARCHAR(255)");
					break;
					
					case 'multiple':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id VARCHAR(1000)");
					break;
					
					case 'numeric':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id NUMERIC(10,2)");
					break;
					
					case 'date':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id DATE");
					break;
					
					case 'textarea':
						DBQuery("ALTER TABLE STUDENTS ADD CUSTOM_$id VARCHAR(5000)");
					break;
					
					case 'log':
					break;
				}
				if($columns['TYPE']!='log' && $columns['TYPE']!='holder' && $columns['TYPE']!='text')
					DBQuery("CREATE INDEX CUSTOM_IND$id ON STUDENTS (CUSTOM_$id)");
				elseif($columns['TYPE']=='text')
					DBQuery("CREATE INDEX CUSTOM_IND$id ON STUDENTS (LOWER(CUSTOM_$id))");					
			}
			elseif($table=='STUDENT_FIELD_CATEGORIES')
			{
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('STUDENT_FIELD_CATEGORIES_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,";
				$values = $id.",";
				$_REQUEST['category_id'] = $id;
			}
			
			$go = false;

			foreach($columns as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
		}

		if($go)
		{
			DBQuery($sql);
			if(key($_REQUEST['tables'])=='new' && $table=='CUSTOM_FIELDS')
				_rearrangeSortOrders($_REQUEST['category_id'],$id,false,$columns['SORT_ORDER']);
		}
	}
	unset($_REQUEST['tables']);
}

if($_REQUEST['modfunc']=='delete')
{
	if($_REQUEST['id'] && ($_REQUEST['id']<100000000 || $_REQUEST['id']>=200000000))
	{
		$field = DBGet(DBQuery("SELECT TYPE,SORT_ORDER FROM CUSTOM_FIELDS WHERE ID='".$_REQUEST['id']."'"));
		if(DeletePrompt(_('student field')))
		{
			$id = $_REQUEST['id'];
			DBQuery("DELETE FROM CUSTOM_FIELDS WHERE ID='$id'");
			_rearrangeSortOrders($_REQUEST['category_id'],$id,$field[1]['SORT_ORDER'],false);
			if($field[1]['TYPE']!='log' && $field[1]['TYPE']!='holder')
				DBQuery("ALTER TABLE STUDENTS DROP COLUMN CUSTOM_$id");
			elseif($field[1]['TYPE']=='log')
				DBQuery("DELETE FROM STUDENT_LOG_ENTRIES WHERE STUDENT_FIELD_ID='".$id."'");
			$_REQUEST['modfunc'] = '';
			unset($_REQUEST['id']);
		}
	}
	elseif($_REQUEST['category_id'] && $_REQUEST['category_id']>3)
	{
		if(DeletePrompt(_('student field category and all fields in the category')))
		{
			$fields = DBGet(DBQuery("SELECT ID,TYPE FROM CUSTOM_FIELDS WHERE CATEGORY_ID='$_REQUEST[category_id]'"));
			foreach($fields as $field)
			{
				DBQuery("DELETE FROM CUSTOM_FIELDS WHERE ID='$field[ID]'");
				if($field['TYPE']!='log' && $field['TYPE']!='holder')
					DBQuery("ALTER TABLE STUDENTS DROP COLUMN CUSTOM_$field[ID]");
				elseif($field['TYPE']=='log')
					DBQuery("DELETE FROM STUDENT_LOG_ENTRIES WHERE STUDENT_FIELD_ID='".$field['ID']."'");
			}
			DBQuery("DELETE FROM STUDENT_FIELD_CATEGORIES WHERE ID='$_REQUEST[category_id]'");
			$_REQUEST['modfunc'] = '';
			unset($_REQUEST['category_id']);
		}
	}
}

if(!$_REQUEST['modfunc'])
{
	// CATEGORIES
	$sql = "SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE";
	$QI = DBQuery($sql);
	$categories_RET = DBGet($QI);

	if(AllowEdit())
	{
		$required_fields = array(200000000,200000001,200000004);
		if($_REQUEST['id']!='new' && $_REQUEST['category_id']!='new')
			$delete_button = "<INPUT type=button value="._('Delete')." onClick='javascript:window.location=\"Modules.php?modname=$_REQUEST[modname]&modfunc=delete&category_id=$_REQUEST[category_id]&id=$_REQUEST[id]\"'>";
		if((!$_REQUEST['id'] && $_REQUEST['category_id'] && $_REQUEST['category_id']<=3) || ($_REQUEST['id'] && (($_REQUEST['id']>=100000000 && $_REQUEST['id']<200000000) || in_array($_REQUEST['id'],$required_fields))))
			$delete_button = '<INPUT type=button value='._('Delete').' DISABLED>';	
//		if(($_REQUEST['id'] && $_REQUEST['id']>=100000000) || (!$_REQUEST['id'] && $_REQUEST['category_id'] && $_REQUEST['category_id']<=3))
	}

	// ADDING & EDITING FORM
	if($_REQUEST['id'] && $_REQUEST['id']!='new')
	{
		$sql = "SELECT * FROM CUSTOM_FIELDS WHERE ID='$_REQUEST[id]'";
		$RET = DBGet(DBQuery($sql));
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['category_id'] && $_REQUEST['category_id']!='new' && $_REQUEST['id']!='new')
	{
		$sql = "SELECT TITLE,ALLOW_TEACHER_MODIFY,SORT_ORDER
				FROM STUDENT_FIELD_CATEGORIES
				WHERE ID='$_REQUEST[category_id]'";
		$RET = DBGet(DBQuery($sql));
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['id']=='new')
		$title = _('New Student Field');
	elseif($_REQUEST['category_id']=='new')
		$title = _('New Student Field Category');
	
	if($_REQUEST['category_id'] && $_REQUEST['category_id']!='new')
	{
		$sql = "SELECT ID,TITLE,TYPE,TYPE AS PLAIN_TYPE,SORT_ORDER FROM CUSTOM_FIELDS WHERE CATEGORY_ID='".$_REQUEST['category_id']."' ORDER BY SORT_ORDER,TITLE";
		$fields_RET = DBGet(DBQuery($sql),array('TYPE'=>'_makeType'));
	}

	if($_REQUEST['id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]";
		if($_REQUEST['id']!='new')
			echo "&id=$_REQUEST[id]";
		echo "&table=CUSTOM_FIELDS method=POST>";

		DrawHeader($title,$delete_button.'<INPUT type=submit value='._('Save').'>');
		$header .= '<TABLE cellpadding=3 width=100%>';
		$header .= '<TR>';

		if($RET['TYPE']!='holder')
			$header .= '<TD>'.TextInput($RET['TITLE'],'tables['.$_REQUEST['id'].'][TITLE]',_('Field Name')) . '</TD>';

		// You can't change a student field type after it has been created
		if($_REQUEST['id']!='new')
		{
			$allow_edit = $_FOCUS['allow_edit'];
			$AllowEdit = $_FOCUS['AllowEdit'][$modname];
			$_FOCUS['allow_edit'] = false;
			$_FOCUS['AllowEdit'][$modname] = false;
		}
		$types = array('radio'=>_('Checkbox'),'text'=>_('Text'),'select'=>_('Pull-Down'),'numeric'=>_('Number'),'multiple'=>_('Select Multiple from Options'),'date'=>_('Date'),'textarea'=>_('Long Text'),'log'=>_('Logging Fields'),'holder'=>_('Placeholder'));
		$header .= '<TD>' . SelectInput($RET['TYPE'],'tables['.$_REQUEST['id'].'][TYPE]',_('Data Type'),$types,false,'onchange="changeDataType(this.options[this.selectedIndex].value)"') . '</TD>';
		if($_REQUEST['id']!='new')
		{
			$_FOCUS['allow_edit'] = $allow_edit;
			$_FOCUS['AllowEdit'][$modname] = $AllowEdit;
		}
		$options = array('-1'=>_('Last'),'1'=>_('First'));
		$holders = 0;
		foreach($fields_RET as $field)
		{
			if($field['PLAIN_TYPE']=='holder')
			{
				$holders++;
				$field['TITLE'] = _('Placeholder').' '.$holders;
			}
			$options[($field['SORT_ORDER']+1)] = _('After').' '.$field['TITLE'];
		}
		$header .= '<TD>' . SelectInput($RET['SORT_ORDER'],'tables['.$_REQUEST['id'].'][SORT_ORDER]',_('Sort Order'),$options,false) . '</TD>';
		//$header .= '<TD>'.TextInput($RET['SORT_ORDER'],'tables['.$_REQUEST['id'].'][SORT_ORDER]',_('Sort Order'),'size=3 maxlength=5').'</TD>';
		foreach($categories_RET as $type)
			$categories_options[$type['ID']] = $type['TITLE'];
	
		if($_REQUEST['id']=='new')
			$header .= '<TD valign=bottom colspan=1><div id=default_div>'.CheckboxInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','',true).'</div><small>'._('Default for new student').'</small></TD>';
		else
		{
			switch($RET['TYPE'])
			{
				case 'radio':
					$header .= '<TD valign=bottom colspan=1><div id=default_div>'.CheckboxInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','',false).'</div><small>'._('Default for new student').'</small></TD>';
				break;
				
				case 'text':
				case 'select':
				case 'numeric':
				case 'multiple':
					$header .= '<TD valign=bottom colspan=1><div id=default_div>'.TextInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','','force').'</div><small>'._('Default for new student').'</small></TD>';
				break;
				
				case 'date':
					$header .= '<TD valign=bottom colspan=1><div id=default_div>'.TextInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','','force').'</div>* YYYY-MM-DD <small>'._('Default for new student').'</small></TD>';
				break;

				case 'textarea':
					$header .= '<TD valign=bottom colspan=1><div id=default_div>'.TextAreaInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','','force').'</div><small>'._('Default for new student').'</small></TD>';
				break;
				
				case 'log':
					$header .= '<TD valign=bottom colspan=1><div id=default_div>'._('N/A').'</div><small>'._('Default for new student').'</small></TD>';
				break;
				
			}
		}
		$header .= '<TD valign=bottom colspan=1><div id=required_div>'.CheckboxInput($RET['REQUIRED'],'tables['.$_REQUEST['id'].'][REQUIRED]','','',($_REQUEST['id']=='new'?true:false)).'</div><small>'._('Required').'</small></TD>';
		if($_REQUEST['id']=='new' || $RET['TYPE']=='text')
			$header .= '<TD>'.TextInput($RET['MAX_LENGTH'],'tables['.$_REQUEST['id'].'][MAX_LENGTH]',_('Max Length'),'size=3 maxlength=10').'</TD>';
		else
			$header .= '<TD>'.NoInput(_('N/A'),_('Max Length')).'</TD>';
		
		$header .= '<TD>' . SelectInput($RET['CATEGORY_ID']?$RET['CATEGORY_ID']:$_REQUEST['category_id'],'tables['.$_REQUEST['id'].'][CATEGORY_ID]',_('Student Field Category'),$categories_options,false) . '</TD>';
		$header .= '</TR><TR id=second_row>';
		$colspan = 2;
		if($RET['TYPE']=='select' || $RET['TYPE']=='multiple')
			$header .= '<TD colspan=2><div id=options_div>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]',_('Pull-Down/Select Multiple Choices').'<BR>* '._('one per line'),'rows=7 cols=40').'</div></TD>';
		elseif($RET['TYPE']=='log')
		{
			$new = true;
			$div = true;
			$header .= '<TD colspan=3><small>'._('Setup the Logging Fields').':</small><TABLE style="border-style:none none none none;" cellspacing=0 border=1 cellpadding=0 width=100%><TR>';
			// IF THE NEW FIELDS ARE MODIFIED IN THE JS SECTION AT TOP, PASTE IN CHANGES HERE
			$header .= '<TD valign=top style="border-color:transparent;border-style:none solid solid none;" bgcolor=#333366><IMG SRC=assets/left_corner.gif><small><B><font color=#FFFFFF>'._('Use Field?').'</font></b></small>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Checkbox').'</font></small><BR>'.CheckBoxInput($RET['LOG_CHECKBOX'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Pull-down').'</font></small><BR>'.CheckBoxInput($RET['LOG_SELECT'],'tables['.$_REQUEST['id'].'][LOG_SELECT]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Date').'</font></small><BR>'.CheckBoxInput($RET['LOG_DATE'],'tables['.$_REQUEST['id'].'][LOG_DATE]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Numeric').'</font></small><BR>'.CheckBoxInput($RET['LOG_NUMERIC'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 1').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT1'],'tables['.$_REQUEST['id'].'][LOG_TEXT1]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 2').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT2'],'tables['.$_REQUEST['id'].'][LOG_TEXT2]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 3').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT3'],'tables['.$_REQUEST['id'].'][LOG_TEXT3]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 4').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT4'],'tables['.$_REQUEST['id'].'][LOG_TEXT4]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Long Text').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXTAREA'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA]','','',$new).'</TD>';

			$header .= '</TR><TR>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;"><small><b>'._('Field Title').'</b></small></TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_CHECKBOX_TITLE'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX_TITLE]','','',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_SELECT_TITLE'],'tables['.$_REQUEST['id'].'][LOG_SELECT_TITLE]','','',$div).'<BR>';
			$header .= '<small><b>Pull-Down Choices:</b></small>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','* '._('one per line'),'cols=10 rows=3');
			$header .= '</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_DATE_TITLE'],'tables['.$_REQUEST['id'].'][LOG_DATE_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_NUMERIC_TITLE'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT1_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT1_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT2_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT2_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT3_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT3_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT4_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT4_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXTAREA_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA_TITLE]','','size=8',$div).'</TD>';
			// END PASTE
			$header .= '</TR></TABLE></TD>';
		}
		elseif($_REQUEST['id']=='new')
			$header .= '<TD colspan=4><div id=options_div></div></TD>';

		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	elseif($_REQUEST['category_id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&table=STUDENT_FIELD_CATEGORIES";
		if($_REQUEST['category_id']!='new')
			echo "&category_id=$_REQUEST[category_id]";
		echo " method=POST>";
		DrawHeader($title,$delete_button.'<INPUT type=submit value='._('Save').'>');
		$header .= '<TABLE cellpadding=3 width=50%>';
		$header .= '<TR>';

		$header .= '<TD>' . TextInput($RET['TITLE'],'tables['.$_REQUEST['category_id'].'][TITLE]',_('Title')) . '</TD>';
		if($_REQUEST['category_id']=='new')
			$new = true;

		$header .= '<TD>' . CheckboxInput($RET['ALLOW_TEACHER_MODIFY'],'tables['.$_REQUEST['category_id'].'][ALLOW_TEACHER_MODIFY]',_('Allow Teachers to Modify this Category'),'',$new) . '</TD>';
		$header .= '<TD>' . TextInput($RET['SORT_ORDER'],'tables['.$_REQUEST['category_id'].'][SORT_ORDER]',_('Sort Order')) . '</TD>';
	
		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	else
		$header = false;

	if($header)
	{
		DrawHeader($header);
		echo '</FORM>';	
	}

	// DISPLAY THE MENU
	$LO_options = array('save'=>false,'search'=>false,'add'=>true);
	
	echo '<TABLE><TR>';
	
	if(count($categories_RET))
	{
		if($_REQUEST['category_id'])
		{
			foreach($categories_RET as $key=>$value)
			{
				if($value['ID']==$_REQUEST['category_id'])
					$categories_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
			}
		}
	}
		
	echo '<TD valign=top>';
	$columns = array('TITLE'=>_('Category'));
	$link = array();
	$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]";
	$link['TITLE']['variables'] = array('category_id'=>'ID');
	$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=new";

	ListOutput($categories_RET,$columns,_('Student Field Category'),_('Student Field Categories'),$link,array(),$LO_options);
	echo '</TD>';
	
	// FIELDS
	if($_REQUEST['category_id'] && $_REQUEST['category_id']!='new' && count($categories_RET))
	{
		echo '<TD valign=top width=75%>';
		
		$count = 0;
		$bgcolor = Preferences('COLOR');
		echo '<TABLE class=EntireBox width=90% cellpadding=6><TR bgcolor='.$bgcolor.'>';
		if(count($fields_RET))
		{
			echo '<b>'.count($fields_RET).' '._('Student Fields').'</b><BR><BR>';
			foreach($fields_RET as $key=>$value)
			{
				if($value['TITLE']=='')
					$value['TITLE'] = '-';
				if($value['ID']==$_REQUEST['id'])
					echo '<TD style="background-color:'.Preferences('HIGHLIGHT').';"><A HREF=Modules.php?modname='.$_REQUEST['modname'].'&category_id='.$_REQUEST['category_id'].'&id='.$value['ID'].'><font color=white>'.$value['TITLE'].'</font></A><BR><small><font color=white>';
				else
					echo '<TD><A HREF=Modules.php?modname='.$_REQUEST['modname'].'&category_id='.$_REQUEST['category_id'].'&id='.$value['ID'].'>'.$value['TITLE'].'</A><BR><small><font color=gray>';
				
				echo $value['TYPE'].'</font></small></TD>';
				echo '</TD>';
				$count++;
				if($count%3==0)
				{
					echo '</TR>';
					if($bgcolor==Preferences('COLOR'))
						$bgcolor = 'F8F8F9';
					else
						$bgcolor = Preferences('COLOR');
					echo '<TR bgcolor='.$bgcolor.'>';
				}
			}
		}
		if(AllowEdit())
		{
			if($count%3!==0)
			{
				if($count%3==2)
					echo '<TD></TD>';
				elseif($count%3==1)
					echo '<TD colspan=2></TD>';

				echo '</TR>';
				if($bgcolor==Preferences('COLOR'))
					$bgcolor = 'F8F8F9';
				else
					$bgcolor = Preferences('COLOR');
				echo '<TR bgcolor='.$bgcolor.'>';
			}

			if($_REQUEST['id']=='new')
			{
				$bgcolor = ' bgcolor=#'.Preferences('HIGHLIGHT');
				$font = '<FONT color=white>';
				$close_font = '</FONT>';
			}
			else
			{
				$bgcolor = '';
				$font = '';
				$close_font = '';
			}
			echo '<TD'.$bgcolor.' colspan=3>'.button('add',($font._('Add a field').$close_font),'Modules.php?modname='.$_REQUEST['modname'].'&category_id='.$_REQUEST['category_id'].'&id=new').'</TD>';
		}
		echo '</TR></TABLE>';

/*		$columns = array('TITLE'=>_('Student Field'),'TYPE'=>_('Data Type'));
		$link = array();
		$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]";
		$link['TITLE']['variables'] = array('id'=>'ID');
		$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]&id=new";
	
		ListOutput($fields_RET,$columns,_('Student Field'),_('Student Fields'),$link,array(),$LO_options);
*/
		echo '</TD>';
	}
	
	echo '</TR></TABLE>';
}

function _makeType($value,$name)
{
	$options = array('radio'=>_('Checkbox'),'text'=>_('Text'),'select'=>_('Pull-Down'),'date'=>_('Date'),'numeric'=>_('Number'),'textarea'=>_('Long Text'),'multiple'=>_('Select Multiple'),'log'=>_('Logging Fields'),'holder'=>_('Placeholder'));
	return $options[$value];
}

/*

*/
function _rearrangeSortOrders($category_id,$field_id,$old,$new=false)
{
	// -1 indicates that the field should be placed at the end of the list
	if($new==-1)
	{
		$last_RET = DBGet(DBQuery("SELECT COALESCE(MAX(SORT_ORDER),0) AS MAX FROM CUSTOM_FIELDS WHERE CATEGORY_ID='$category_id'"));
		// SET THE SORT ORDER TO 1 MORE THAN THE CURRENT MAX, UNLESS THE NEW FIELD IS THE FIRST TO BE CREATED IN THIS TAB
		if($last_RET[1]['MAX']!=-1)
			$new = ($last_RET[1]['MAX']+1);
		else
			$new = 1;
	}

	// we're adding this field
	if(!$old)
	{
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER=(SORT_ORDER+1) WHERE SORT_ORDER>='$new' AND CATEGORY_ID='$category_id'");
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER='$new' WHERE ID='$field_id'");
	}
	// old has been deleted
	elseif($old && $new===false)
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER=(SORT_ORDER-1) WHERE SORT_ORDER>'$old' AND CATEGORY_ID='$category_id'");	
	// field hasn't been moved at all
	elseif($old==$new)
		return;
	// field has been pushed down the page
	elseif($old<$new)
	{
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER=(SORT_ORDER-1) WHERE SORT_ORDER>'$old' AND SORT_ORDER<'$new' AND CATEGORY_ID='$category_id'");
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER='".($new-1)."' WHERE ID='$field_id'");
	}
	// field has been pushed up the page 
	elseif($old>$new)
	{
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER=(SORT_ORDER+1) WHERE SORT_ORDER<'$old' AND SORT_ORDER>='$new' AND CATEGORY_ID='$category_id'");
		DBQuery("UPDATE CUSTOM_FIELDS SET SORT_ORDER='$new' WHERE ID='$field_id'");
	}
}
?>